package com.shire.shire_interface.controller.group;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxBlacklist;
import com.shire.shire_interface.service.group.impl.WxBlacklistService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/group")
public class WxUserBlackListController extends BaseController {
    @Resource
    WxBlacklistService blacklistService;

    @ResponseBody
    @PostMapping(value = "/blacklist")
    public Response getBlackList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
//            Integer csId = jsonObject.getInteger("csId");
//
//            if (!checkLoginUserByCsId(session, csId)){
//                response.error("请登录！");
//                response.setCode(501);
//                return response;
//            }

            List<WxBlacklist> list = blacklistService.getBlackList();
            response.success("成功");
            response.setData(list);
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

    @ResponseBody
    @PostMapping(value = "/addblacklist")
    public Response addBlackList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
//            Integer csId = jsonObject.getInteger("csId");
//
//            if (!checkLoginUserByCsId(session, csId)){
//                response.error("请登录！");
//                response.setCode(501);
//                return response;
//            }

            String userStr = jsonObject.getString("userList");
            String[] userArr = userStr.split(",");
            List<WxBlacklist> list = new ArrayList<>();
            for (int i = 0; i<userArr.length; i++){
                WxBlacklist userItem = new WxBlacklist();
                userItem.setNickname(userArr[i]);
                list.add(userItem);
            }

            blacklistService.insertBlackList(list);
            response.setData(list);
            response.success("成功");

        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
